﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace OnlineManagement.Web.Account
{
    public partial class UserMgr : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnDelUser.Click += new EventHandler(btnDelUser_Click);
            btnChangPwd.Click += new EventHandler(btnChangPwd_Click);
            if(!Page.IsPostBack){
                string userName = HttpContext.Current.User.Identity.Name;
                if(!Roles.IsUserInRole(userName,Common.PubConstant.SuperAdmin)){
                    return;
                }
                MembershipUserCollection users = Membership.GetAllUsers();
                rptUsers.DataSource = users;
                rptUsers.DataBind();
            }
        }

        void btnChangPwd_Click(object sender, EventArgs e)
        {
            string userName = lbChangePwd.Text;
            string pwd = txtPwd.Text;
            string confirmPwd = txtConrimPwd.Text;
            try
            {
                if (!string.IsNullOrEmpty(pwd) && pwd.Equals(confirmPwd))
                {
                    MembershipUser users = Membership.GetUser(userName);
                    string newPwd = users.ResetPassword();
                    bool result = users.ChangePassword(newPwd, pwd);
                    lbChangePwdMsg.Text = result ? "修改成功" : "修改失败";
                }
            }catch(Exception ex){
                lbChangePwdMsg.Text = ex.Message;
            }
        }

        void btnDelUser_Click(object sender, EventArgs e)
        {
            string userName = lbDelUserName.Text;
            if(string.IsNullOrEmpty(userName)){
                lbDelMsg.Text = "请选择要删除的用户";
                return;
            }
            try
            {
              bool result=  Membership.DeleteUser(userName);
                if(result){
                    lbDelMsg.Text = "删除成功";
                }
            }catch(Exception ex){
                lbDelMsg.Text = "服务器发生错误:"+ex.Message;
            }
        }
    }
}